package dao;

import pojo.DVD;

import java.time.LocalDate;
import java.util.Arrays;

/**
 * DVD操作类
 */
public class DvdDaoImpl implements DvdDao {
    //初始化存储DVD的容器  默认最多只能存储10个DVD,同时，默认dvd是从前往后排列，中间没有null元素
    static DVD[] dvds = new DVD[5];

    static {
        dvds[0] = new DVD("罗马假日", 0, LocalDate.of(2022, 1, 10), 15, 4);
        dvds[1] = new DVD("风声鹤唳", 1, null, 12, 3);
        dvds[2] = new DVD("笑傲江湖", 1, null, 30, 5);
        dvds[3] = new DVD("杀死比尔", 1, null, 25, 6);
    }

    /**
     * 查询货架上所有的DVD
     * @return
     */
    @Override
    public DVD[] queryAll() {
        DVD[] dvds1 = null;
        //标志   表示数组中是否还有空元素   如果没有空元素（true），表示货架已满，直接将货架全部元素返回
        boolean flag = true;
        for (int i = 0; i < dvds.length; i++) {
            //如果货架上有空元素，说明只需将此空元素之前的dvd返回即可，同时修改flag为false
            if (dvds[i] == null) {
                dvds1 = Arrays.copyOf(dvds, i);
                flag = false;
                break;
            }
        }
        if (flag){
            return dvds;
        }
        return dvds1;
    }

    /**
     * 添加DVD到货架
     * @param dvd
     * @return 添加结果  boolean类型
     */
    @Override
    public boolean insertDVD(DVD dvd) {
        boolean flag = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] == null) {
                dvds[i] = dvd;
                flag = true;
                break;
            }
        }
        return flag;
    }

    @Override
    public boolean deleteDVD(String name) {

        boolean flag = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] != null && dvds[i].getName().equals(name)) {
                for (int j = i; j < dvds.length - 1; j++) {
                    dvds[i] = dvds[i + 1];
                }
                flag = true;
                dvds[dvds.length - 1] = null;
                break;
            }
        }
        return flag;
    }

    //借阅
    @Override
    public boolean lendDVD(DVD dvd) {
        boolean flag = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] != null && dvds[i].getName().equals(dvd.getName())) {
                long count = dvds[i].getCount() + 1;
                dvds[i].setCount(count);
                dvds[i].setState(0);
                dvds[i].setDate(LocalDate.now());
                flag = true;
                break;
            }
        }
        return flag;
    }

    //根据dvd名字查询dvd
    @Override
    public DVD queryByName(String name) {
        DVD dvd = null;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] != null && dvds[i].getName().equals(name)) {
                dvd = dvds[i];
                break;
            }
        }
        return dvd;

    }


    //归还
    @Override
    public int revert(DVD dvd) {
        boolean flag = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] != null && dvds[i].getName().equals(dvd.getName())) {
                dvds[i].setState(1);
                dvds[i].setDate(null);
                break;
            }
        }
        return 0;
    }
}
